www.gusucode.com > Phased Array System Toolbox Add-On for Demorad 工具箱matlab源码程序 > Phased Array System Toolbox Add-On for Demorad/demorad/demorad/DemoradBoard.m

    classdef DemoradBoard < AbstractRadarBoard
  %DemoradBoard Receive FMCW echoes from a Demorad Radar Sensor Platform.
  % H = DemoradBoard creates a Demorad Radar Sensor Platform System object, H,
  % that enables communication between MATLAB(R) and an Analog Devices(R)
  % Demorad Radar Sensor Platform. The Demorad Radar Sensor Platform object
  % enables communication with a Demorad Radar Sensor Platform connected via USB
  % port on the host computer. This object receives and sends signal and control
  % data from the Demorad using Analog Devices(R) driver software. Although the
  % Demorad Radar Sensor Platform System object receives samples from a Demorad,
  % the object acts as a signal source that outputs a complex-valued matrix
  % signal of fixed size and dimension.
  %
  % H = DemoradBoard(Name,Value) creates Demorad Radar Sensor Platform object,
  % H, with the specified property Name set to the specified Value. You can
  % specify additional name-value pair arguments in any order as
  % (Name1,Value1,...,NameN,ValueN).
  %
  % Step method syntax:
  %
  % Y = step(H) receives signal data from the Demorad Radar Sensor Platform.
  % Output signal, Y, is an Nx4xM matrix of complex samples, where each column
  % represents an element of the Demorad receive array, N is the specified
  % number of samples per frame defined by the SAMPLESPERFRAME property, and M
  % is the number of chirps, defined by the NUMCHIRPS property.
  %
  % DemoradBoard methods:
  %
  % step     - Return data from a transmitted pulse
  % release  - Allow property value and input characteristics changes
  % reset    - Reset operation time
  % isDone   - Operating status (logical)
  % isLocked - Locked status    (logical)
  %
  % DemoradBoard properties:
  %
  % TransmitPower           - Transmit power (0 - 100)
  % NumChirps               - Number of returned chirps (1 - 128)
  % SamplesPerFrame         - Number of samples per frame
  % AcquisitionTime         - Number of seconds to send/receive chirps
  % WindowMethod            - Method for windowing received samples
  % RampTime                - Chirp ramp time in seconds
  % PRI                     - Pulse repetition interval in seconds
  % StartFrequency          - Chirp start frequency in Hz
  % StopFrequency           - Chirp stop frequency in Hz
  %
  % Metadata                - A struct with fields of RampTime, PRI, StartFrequency,
  %                           StopFrequency, and SweepSlope             (read-only)
  % SampleRate              - Demorad Radar Sensor Platform sample rate (read-only)
  % CenterFrequency         - FMCW chirp center frequency               (read-only)
  % NumChannels             - Number of channels (elements)             (read-only)
  % ReceiveElementSpacing   - Spacing of receive elements in meters     (read-only)
  % TransmitElementSpacing  - Spacing of transmit elements in meters    (read-only)
  % TimeResolution          - Time between returned samples in seconds  (read-only)
  %
  % % Example:
  % %  Use the default configuration of the Demorad Radar Sensor Platform to
  % %  transmit and receive FMCW pulses for 10 seconds. The received pulses are
  % %  stored in a radar data cube.
  %
  % demorad = DemoradBoard('AcquisitionTime',10);
  % logger = dsp.SignalSink('FrameHandlingMode','3-D array (separate)');
  % while ~isDone(demorad)
  %   data = demorad();
  %   logger(data);
  % end
  
  % Copyright 2019 The MathWorks Inc.
  
  properties(Access = public,Nontunable)
    %TransmitPower    Transmit power
    %   The value of this property is used as the amplitude calibration code for
    %   the Demorad, and can be set to any value between 0 and 100 inclusive. 0
    %   corresponds to an output power of -20 dBm, while 100 corresponds to 8
    %   dBm. See the data sheet for the Analog Devices(R) ADF5901 for more 
    %   information on the intermediate values.
    TransmitPower = 100;
  end
  
  properties (SetAccess = protected)
    %SampleRate   Sample rate of the radar board ADC
    %   The value of this property is the sample rate of the ADC on the radar
    %   board. This value is read directly from the hardware, and requires a
    %   connection before being calculated.
    SampleRate
  end
  
  properties (Constant)
    %NumChannels   Number of antenna elements
    %   The value of this property is the number of antenna elements that make 
    %   up the phased array on the Demorad. The Demorad has 4 antenna elements
    %   arranged as a uniform linear array.
    NumChannels = 4
    %ReceiveElementSpacing   Receive element spacing (m)
    %   The value of this property is the spacing of the receive elements of
    %   the array (in meters). The receive elements of the Demorad array are
    %   spaced every 1/2 wavelength of the operating frequency.
    ReceiveElementSpacing = 6.2e-3
    %TransmitElementSpacing   Transmit element spacing (m)
    %   The value of this property is the spacing of the transmit elements
    %   of the array (in meters). The transmit elements of the Demorad array 
    %   are spaced 3/2 wavelength of the operating frequency apart.
    TransmitElementSpacing = 3*6.2e-3
  end
  
  properties (Dependent, SetAccess = private)
    %TimeResolution   Time between successive reads (s)
    %   The value of this property is the time between reading the samples from
    %   the radar hardware into MATLAB.
    TimeResolution
  end
  
  properties (Access = private)
    StrtIdx = 0;                                % Start index for config
    StopIdx                                     % Stop index for config
    CalibrationData
  end
  
  properties (Hidden, Constant)
    MeasurementMode = 'Adi';
    BoardName = 'Demorad'
    MeasurementModeSet = matlab.system.StringSet({'Adi'});
  end
  
  methods
    function obj = DemoradBoard(varargin)
      %DemoradBoard Receive FMCW echoes from a Demorad Radar Sensor Platform.
      % H = DemoradBoard creates a Demorad Radar Sensor Platform System object, H,
      % that enables communication between MATLAB(R) and an Analog Devices(R)
      % Demorad Radar Sensor Platform. The Demorad Radar Sensor Platform object
      % enables communication with a Demorad Radar Sensor Platform connected via USB
      % port on the host computer. This object receives and sends signal and control
      % data from the Demorad using Analog Devices(R) driver software. Although the
      % Demorad Radar Sensor Platform System object receives samples from a Demorad,
      % the object acts as a signal source that outputs a complex-valued matrix
      % signal of fixed size and dimension.
      %
      % H = DemoradBoard(Name,Value) creates Demorad Radar Sensor Platform object,
      % H, with the specified property Name set to the specified Value. You can
      % specify additional name-value pair arguments in any order as
      % (Name1,Value1,...,NameN,ValueN).
      %
      % Step method syntax:
      %
      % Y = step(H) receives signal data from the Demorad Radar Sensor Platform.
      % Output signal, Y, is an Nx4xM matrix of complex samples, where each column
      % represents an element of the Demorad receive array, N is the specified
      % number of samples per frame defined by the SAMPLESPERFRAME property, and M
      % is the number of chirps, defined by the NUMCHIRPS property.
      %
      % DemoradBoard methods:
      %
      % step     - Return data from a transmitted pulse
      % release  - Allow property value and input characteristics changes
      % reset    - Reset operation time
      % isDone   - Operating status (logical)
      % isLocked - Locked status    (logical)
      %
      % DemoradBoard properties:
      %
      % TransmitPower           - Transmit power (0 - 100)
      % NumChirps               - Number of returned chirps (1 - 128)
      % SamplesPerFrame         - Number of samples per frame
      % AcquisitionTime         - Number of seconds to send/receive chirps
      % WindowMethod            - Method for windowing received samples
      % RampTime                - Chirp ramp time in seconds
      % PRI                     - Pulse repetition interval in seconds
      % StartFrequency          - Chirp start frequency in Hz
      % StopFrequency           - Chirp stop frequency in Hz
      %
      % Metadata                - A struct with fields of RampTime, PRI, StartFrequency,
      %                           StopFrequency, and SweepSlope             (read-only)
      % SampleRate              - Demorad Radar Sensor Platform sample rate (read-only)
      % CenterFrequency         - FMCW chirp center frequency               (read-only)
      % NumChannels             - Number of channels (elements)             (read-only)
      % ReceiveElementSpacing   - Spacing of receive elements in meters     (read-only)
      % TransmitElementSpacing  - Spacing of transmit elements in meters    (read-only)
      % TimeResolution          - Time between returned samples in seconds  (read-only)
      %
      % % Example:
      % %  Use the default configuration of the Demorad Radar Sensor Platform to
      % %  transmit and receive FMCW pulses for 10 seconds. The received pulses are
      % %  stored in a radar data cube.
      %
      % demorad = DemoradBoard('AcquisitionTime',10);
      % logger = dsp.SignalSink('FrameHandlingMode','3-D array (separate)');
      % while ~isDone(demorad)
      %   data = demorad();
      %   logger(data);
      % end
  
      % Support name-value pair arguments when constructing object
      setProperties(obj,nargin,varargin{:})
      if ~exist('Adf24Tx2Rx4','file')
        error('DemoradBoard:MissingDrivers',...
          ['Could not find Analog Devices provided drivers.' newline ...
          'Please add their parent directory to the MATLAB path.']);
      end
      obj.RadarBoard = Adf24Tx2Rx4();
    end
    
    function val = get.TimeResolution(obj)
      val = 128*obj.PRI; % Demorad returns data after 128 chirps
    end
  end
  
  methods(Access = protected)
    function validatePropertiesImpl(obj)
      % restrictions on pulse repetition interval and ramp time
      if obj.RampTime < 260e-6 || obj.PRI - obj.RampTime < 10e-6
        error('DemoradBoard:InvalidPRI', ...
          ['Expected pulse repetition interval to be at least 10 microseconds ' ...
          'longer than the ramp time.']);
      end
    end
      
    function rngIQ = acquireData(obj)
      % Read I/Q frames. If this fails, then there is a connection issue
      iq = obj.RadarBoard.BrdGetData();
      if isempty(iq)
        error('DemoradBoard:NoData',['Error receiving samples from hardware. ' ...
          'Please try resetting board.']);
      end
      
      % get data for M chirps and N channels.
      % returned format is [fast-time, spatial, slow-time]
      rawData = permute(reshape(iq,256,[],4),[1 3 2]);  
      rngIQ = rawData.*obj.RadarBoard.FuSca.*obj.CalibrationData;
    end
    
    function poweredOn = powerOnBoard(obj)
      % Turn on board
      if ~DemoradBoard.InUse('status')
        obj.RadarBoard.BrdRst();
        if obj.RadarBoard.UsbOpen
          % Board was powered on successfully
          poweredOn = true;
          obj.SampleRate = obj.RadarBoard.Get('fs');
          DemoradBoard.InUse('toggle');
        else
          poweredOn = false;
        end
      else
        error('DemoradBoard:InterfaceUnavailable', ['Another interface to ' ...
          'the Demorad is currently open. Only one interface may be open' ...
          ' at a time. Delete or release the currently in-use interface.']);
      end
    end
    
    function poweredOff = powerOffBoard(obj)
      try
        % copied from UsbAdi to close connection to USB        
        if obj.RadarBoard.UsbOpen && DemoradBoard.InUse('status')
          obj.RadarBoard.UsbOpen     =   0;
          DemoradBoard.InUse('toggle');
          DemoRadUsb('Close');
        end
%         obj.RadarBoard.delete(); % deal with no power down API
%         obj.RadarBoard = Adf24Tx2Rx4();
        poweredOff = true;
      catch
        poweredOff = false;
      end
    end
    
    function success = configureBoard(obj)
      % instantiate the board drivers

      % Calculate configuration parameters
      obj.StopIdx = obj.NumChirps;
      
      % Configure Demorad board
      cfg.fStrt = obj.StartFrequency;
      cfg.fStop = obj.StopFrequency;
      cfg.TRampUp = obj.RampTime;
      cfg.Tp = obj.PRI;
      cfg.N = obj.SamplesPerFrame; %ignored
      cfg.StrtIdx = obj.StrtIdx;
      cfg.StopIdx = obj.StopIdx;
      cfg.NrFrms = floor(obj.AcquisitionTime/obj.PRI);
      
      % Configure Receiver
      obj.RadarBoard.RfRxEna()
      
      % Configure Transmitter (Antenna 0 - 2 channels, Power 0 - 100)
      obj.RadarBoard.RfTxEna(1, obj.TransmitPower);
      
      cal = obj.RadarBoard.BrdGetCalDat();
      if isempty(cal)
        success = false;
        return;
      end
      
      obj.CalibrationData = ...
        ones(256,1,obj.NumChirps).*cal(1:obj.NumChannels).';
      
      warning('off',''); % suppress driver warning about ramp-time
      obj.RadarBoard.RfMeas(obj.MeasurementMode, cfg);
      warning('on',''); 
      
      % Update the sample frequency
      obj.SampleRate = obj.RadarBoard.Get('fs');
      success = true;
    end
  end
  
  methods (Access = private,Static)
    function used = InUse(action)
      % only one instance can connect to the Demorad at a time. Check to make
      % sure that there is only one object accessing the Demorad.
      persistent inUse;
      if isempty(inUse)
        inUse = false;
      end
      if strcmpi(action,'toggle')
        used = ~inUse;
        inUse = ~inUse; % toggle
      elseif strcmpi(action,'status')
          used = inUse;
      end
    end
  end
end